package leetcode.singles;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: Liu Yue
 * @Descripition: 容器式单例
 * @Date; Create in 2020/3/1 14:31
 **/
public class ContainerSingleton {

    private ContainerSingleton(){}

    private static Map<String,Object> ioc = new ConcurrentHashMap<String,Object>();

    public static Object getInstance(String className){
        if (!ioc.containsKey(className)){
            try {
                Object o = Class.forName(className).newInstance();
                ioc.put(className,o);
                return o;
            }catch (Exception e){
                e.printStackTrace();
            }
        }else {
            return ioc.get(className);
        }
        return null;
    }
}
